package com.csust.automaticteach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.automaticteach.demo.zhipu.MindMapClient;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.entity.MindMap;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.service.MindMapService;
import com.csust.automaticteach.mapper.MindMapMapper;
import com.csust.automaticteach.service.UserService;
import com.spark.client.SparkClient;
import com.spark.client.config.SparkConfig;
import com.spark.client.exception.SparkClientException;
import com.spark.client.model.SparkResponse;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.UUID;

import static com.csust.automaticteach.constant.AppPromptConstant.MIND_MAP_SYSTEM_ROLE;

/**
* @author 钰玟
* @description 针对表【t_mind_map(思维导图表)】的数据库操作Service实现
* @createDate 2025-08-12 14:56:18
*/
@Service
public class MindMapServiceImpl extends ServiceImpl<MindMapMapper, MindMap>
    implements MindMapService{
    @Resource
    private MindMapClient mindMapClient;
    @Resource
    private UserService userService;
    @Resource
    private SparkConfig sparkConfig;

    /**
     * 生成思维导图文本
     * @param prompt 用户提示词
     * @param chatId 会话ID
     * @param request 请求
     * @return 生成的思维导图文本
     */
    @Override
    public Flux<String> genMindMapTextByZhipu(String prompt, String chatId, HttpServletRequest request) {
        Flux<String> stringFlux = mindMapClient.genMindMapByStream(prompt, chatId);
        User loginUser = userService.getLoginUser(request);
        //创建一个StringBuilder对象，用于拼接生成的流式Markdown代码
        StringBuilder markdownCode = new StringBuilder();
        return stringFlux.doOnNext(markdownCode::append)// 将每个块拼接起来
                  .doOnComplete(()->{
            String completeMarkdown = markdownCode.toString();
            //数据库操作 : 存储至数据库中
            MindMap mindMap = new MindMap();
            mindMap.setContent(completeMarkdown);
            mindMap.setReference(prompt);
            mindMap.setUserId(loginUser.getId());
            mindMap.setTitle("思维导图" + UUID.randomUUID().toString().substring(0, 4));
            boolean save = save(mindMap);
            ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
        });
    }

    @Override
    public Flux<String> genMindMapTextBySpark(String prompt, String chatId, HttpServletRequest request) {
        Flux<String> stringFlux = null;
        try {
            stringFlux = SparkClient.builder(sparkConfig).build()
                    .system(MIND_MAP_SYSTEM_ROLE)
                    .user(prompt)
                    .stream()
                    .map(SparkResponse::getDeltaContent);
        } catch (SparkClientException e) {
            throw new RuntimeException(e);
        }
        User loginUser = userService.getLoginUser(request);
        //创建一个StringBuilder对象，用于拼接生成的流式Markdown代码
        StringBuilder markdownCode = new StringBuilder();
        return stringFlux.doOnNext(markdownCode::append)// 将每个块拼接起来
                .doOnComplete(()->{
                    String completeMarkdown = markdownCode.toString();
                    //数据库操作 : 存储至数据库中
                    MindMap mindMap = new MindMap();
                    mindMap.setContent(completeMarkdown);
                    mindMap.setReference(prompt);
                    mindMap.setUserId(loginUser.getId());
                    mindMap.setTitle("思维导图" + UUID.randomUUID().toString().substring(0, 4));
                    boolean save = save(mindMap);
                    ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
                });
    }
}




